package com.tedu.redisdemoo.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Serializable> initReadisTemplate(
            RedisConnectionFactory redisConnectionFactory
    ){
        // 初始化Redis 模板类
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        // 为模板类设置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // key 序列化为字符串显示
        redisTemplate.setKeySerializer(RedisSerializer.string());
        // 设置Valus的序列化方式
        redisTemplate.setValueSerializer(RedisSerializer.json());
        // 设置hash的key序列化方式
        redisTemplate.setHashKeySerializer(RedisSerializer.json());
        redisTemplate.setHashValueSerializer(RedisSerializer.json());

        return redisTemplate;

    }


}
